package com.hsy.baoinbao.modules.my.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/23.
 */
public class ChangePhoneActivity extends CommonBaseActivityWithSimpleActionBar {
    private final long TIMER_LENGTH = 60000l;
    private final long TIMER_SEQUENCE = 1000l;
    private TextView change_phone_get_code, finish;
    private TimeCount mTimeCount;
    private EditText phone_edit, code;
    private DPHttpUtil.HttpCallback httpCallBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                DPLogUtil.d(response);
            } else if (1 == what) {
                DPLogUtil.d(response);
                showToast("修改成功");
                finish();
            }

        }

        @Override
        public void onError(int what, int type, VolleyError error) {

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);
        setTitle("更换手机");
        change_phone_get_code = F(R.id.change_phone_get_code);
        finish = F(R.id.finish);
        phone_edit = F(R.id.phone_edit);
        code = F(R.id.code);
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEmpty(phone_edit.getText().toString())) {
                    showToast("请填写手机号");
                    return;
                } else if (isEmpty(code.getText().toString())) {
                    showToast("请填写验证码");
                    return;
                }
                Map<String, String> data = new HashMap<>();
                data.put("token", CommonUtils.getToken(ChangePhoneActivity.this));
                data.put("username", phone_edit.getText().toString());
                data.put("smscode", code.getText().toString());
                HttpUtil.httpPostWithParams(ChangePhoneActivity.this, 1, Consts.API_PHONE, data, httpCallBack);
            }
        });
        change_phone_get_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEmpty(phone_edit.getText().toString())) {
                    showToast("请填写手机号");
                    return;
                }
                mTimeCount = new TimeCount(TIMER_LENGTH, TIMER_SEQUENCE);
                mTimeCount.start();
                Map<String, String> data = new HashMap<>();
                data.put("mobile", phone_edit.getText().toString());
                HttpUtil.httpPostWithParams(ChangePhoneActivity.this, 0, Consts.API_SEND_SMS, data, httpCallBack);
            }
        });

    }

    class TimeCount extends CountDownTimer {
        String sendAgain = "重新获取";

        boolean isRunning = false;

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (millisUntilFinished / 1000 == 0) {
                onFinish();
            } else {
                isRunning = true;
                change_phone_get_code.setText(sendAgain + "(" + millisUntilFinished / 1000 + "s" + ")");
                change_phone_get_code.setClickable(false);
                change_phone_get_code.setBackgroundResource(R.drawable.frame_person_verification_code_gary);
            }
        }

        @Override
        public void onFinish() {
            isRunning = false;
            String str = "获取验证码";
            change_phone_get_code.setText(str);
            change_phone_get_code.setClickable(true);
            change_phone_get_code.setBackgroundResource(R.drawable.frame_person_verification_code);
        }
    }
}
